package com.jiyun.service;

import com.jiyun.entity.UsianUser;
import com.jiyun.mapper.UsianUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UsianUserService {

    @Autowired
    UsianUserMapper usianUserMapper;

    //根据用户名和密码查询登录

    public boolean login(UsianUser user){

        /*
        1、现根据用户名查询该对象信息
        2、然后判断是否为空
                空：返回false
                存在：判断密码
                    2.1 先将页面发送来的密码进行加密
                    2.2 再将根据用户名存到的密码与之判断

        * */
        //根据用户名查询对象信息
        UsianUser usianUser = new UsianUser();
        usianUser.setUserName(user.getUserName());
        List<UsianUser> userList = usianUserMapper.select(usianUser);
        //判断是否存在
        if (userList==null) {
            return  false;
        }else {
            //存在对象信息

            //加密
            String str = "woaijiyun";
            StringBuilder stringBuilder = new StringBuilder(user.getPassword());
            stringBuilder.insert(user.getPassword().length()-1,str);

            System.out.println(stringBuilder);
            //判断
            String password = userList.get(0).getPassword();

            if (password.equals(stringBuilder.toString())) {
                return true;
            }else {
                return false;
            }


        }


    }


    public void addUsianUser(UsianUser user) {
        //加密
        String str = "woaijiyun";

        StringBuilder stringBuilder = new StringBuilder(user.getPassword());
        stringBuilder.insert(user.getPassword().length()-1,str);

        user.setPassword(stringBuilder.toString());

        usianUserMapper.insert(user);
    }
}
